/* 
1、已知persons.json用户信息：

[{"username":"xm1","password":"1234"},{"username":"xm2","password":"222"},{"username":"xm3","password":"333"}]

2、现有 let user = { username:"admin1",password:"12345" }



登录原理：创建login.js文件
    如果persons.json中含有对应的用户名和密码则提示登录成功，否则提示用户名和密码错误

*/


// 引入fs模块
let fs = require("fs");

// 登录
// 1.从person.json中读取数据
let data = fs.readFileSync("./persons.json", { "encoding": "utf-8" });
console.log(data, typeof data);  //获取到的数据是JSON格式的字符串

let user = { "username": "xm3", "password": "333" }

// 2.转为JSON格式的对象
let persons = JSON.parse(data);


//3.判断
let resUser = persons.find(val =>
    val.username == user.username && val.password == user.password // 条件
);


if (resUser) {
    console.log("登录成功");
} else {
    console.log("用户名或密码错误");
}


